
//
//  HttpsTool.m
//  ShakeMountain
//
//  Created by KyのMacPro  on 19/1/5.
//  Copyright © 2019年 KyのMacPro. All rights reserved.
//

#import "HttpsTool.h"
#import "AFNetworking.h"
@interface HttpsTool ()

@property (nonatomic, strong) AFHTTPSessionManager *manager;

@end
@implementation HttpsTool
#pragma mark--GET
+(void)get:(NSString *)url params:(NSDictionary *)params success:(successResposeBlock)successHandler failure:(failureResponseBlock)failureHandler{
    NSMutableDictionary *paramsM = [NSMutableDictionary dictionaryWithDictionary:params];
    AFHTTPSessionManager *mgr = [self getRequstManager];
    [mgr GET:[Url stringByAppendingString:url] parameters:paramsM progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self prettyPrintAboutHttpWithOperation:task withResponseObj:responseObject withManager:mgr];
         !successHandler?:successHandler(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        !failureHandler?:failureHandler(error);
        
    }];
}
#pragma mark--POST
+(void)post:(NSString *)url params:(NSDictionary *)params success:(successResposeBlock)successHandler failure:(failureResponseBlock)failureHandler{
    NSMutableDictionary *paramsM = [NSMutableDictionary dictionaryWithDictionary:params];
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];
    AFHTTPSessionManager *mgr = [self getRequstManager];
    [mgr POST:[Url stringByAppendingString:url] parameters:paramsM progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self prettyPrintAboutHttpWithOperation:task withResponseObj:responseObject withManager: mgr];
        !successHandler?:successHandler(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {!failureHandler?:failureHandler(error);
    }];
    
}
#pragma mark--DELETE
+(void)DELETE:(NSString *)url params:(NSDictionary *)params success:(successResposeBlock)successHandler failure:(failureResponseBlock)failureHandler{
    NSMutableDictionary *paramsM = [NSMutableDictionary dictionaryWithDictionary:params];
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];
     AFHTTPSessionManager *mgr = [self getRequstManager];
    [mgr DELETE:[Url stringByAppendingString:url] parameters:paramsM success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self prettyPrintAboutHttpWithOperation:task withResponseObj:responseObject withManager: mgr];
        !successHandler?:successHandler(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        !failureHandler?:failureHandler(error);
    }];
}
#pragma mark--PUT
+(void)PUT:(NSString *)url params:(NSDictionary *)params success:(successResposeBlock)successHandler failure:(failureResponseBlock)failureHandler{
    NSMutableDictionary *paramsM = [NSMutableDictionary dictionaryWithDictionary:params];
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];
    AFHTTPSessionManager *mgr = [self getRequstManager];
    [mgr PUT:[Url stringByAppendingString:url] parameters:paramsM success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self prettyPrintAboutHttpWithOperation:task withResponseObj:responseObject withManager: mgr];
        !successHandler?:successHandler(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         !failureHandler?:failureHandler(error);
    }];
}
#pragma mark - post 上传图片-
+(void)uploadAvatarImgWithUrl:(NSString *)url WithImg:(UIImage *)avatarImg fileName:(NSString *)fileName success:(successResposeBlock)successHandler failure:(failureResponseBlock)failureHandler
{
    AFHTTPSessionManager *mgr = [self getRequstManager];
    NSData *imgData = UIImageJPEGRepresentation(avatarImg, 0.5);
    [mgr POST:[Url stringByAppendingString:url] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:imgData name:@"file" fileName:fileName mimeType:@"image/jpeg"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self prettyPrintAboutHttpWithOperation:task withResponseObj:responseObject withManager:mgr];
        !successHandler?:successHandler(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {!failureHandler?:failureHandler(error);}];
}

#pragma mark- AFHTTPRequestOperationManager初始化 网络超时设置 请求头
+(AFHTTPSessionManager *)getRequstManager
{

    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    [mgr setRequestSerializer:[AFJSONRequestSerializer serializer]];
    //如果报接受类型不一致请替换一致text/html
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"text/html",@"text/xml",@"text/json",@"text/plain",@"text/JavaScript",@"application/json",@"image/jpeg",@"image/png",@"application/octet-stream", nil];
    NSUserDefaults *User = [NSUserDefaults standardUserDefaults];

    // 设置token
    if (![User objectForKey:@"token"]) {
    }else{
        [mgr.requestSerializer setValue:[User objectForKey:@"token"] forHTTPHeaderField:@"Authorization"];
    }
    // 请求超时设定
    mgr.requestSerializer.timeoutInterval = 15;
    return mgr;
}
#pragma mark- 打印url 和 json数据 httpHeader用于调试
+(void)prettyPrintAboutHttpWithOperation:(NSURLSessionDataTask *)operation withResponseObj:(id)responseObj withManager:(AFHTTPSessionManager *)mgr
{
#if DEBUG
////     请求的url
//    NSLog(@"fianlUrl: %@", operation.response.URL.absoluteString);
//
////     请求的json数据打印
//    NSData *dataJson = [NSJSONSerialization dataWithJSONObject:responseObj options:NSJSONWritingPrettyPrinted error:nil];
//    NSString *jsonStr = [[NSString alloc] initWithData:dataJson encoding:NSUTF8StringEncoding];
//    NSLog(@"dataJson: %@", jsonStr);
//
////     请求的header信息
//    NSDictionary *dict = mgr.requestSerializer.HTTPRequestHeaders;
//    NSLog(@"请求头httpHeader: %@", dict);
#endif
}

@end
